<template>
	<div style="padding:0px;">
		<el-card class="box-card">
		  <div  class="text item">
			  <el-form :model="dialogForm" :label-width="formLabelWidth" :rules="rules" ref='dialogForm'>
			    <el-form-item label="最长天数" prop="maximum">
			      <el-input-number v-model="dialogForm.maximum" :min="1"  :disabled="!dialogSendData.isAdd"></el-input-number>
			    </el-form-item>
			    <el-form-item label="最短天数" prop="minimum">
			      <el-input-number v-model="dialogForm.minimum" :min="1" :max="dialogForm.maximum" :disabled="!dialogSendData.isAdd"></el-input-number>
			    </el-form-item>
		       <el-form-item label="借款周期类型"  prop="type" >    
				    <el-select 
		               v-model="dialogForm.type" 
		               placeholder="借款周期类型" 
		               :disabled="!dialogSendData.isAdd" 
		             >
		             <el-option  label="借款周期类型" value="" >
                     </el-option>
	                 <el-option v-for="(item, index) in type" :label="item" :value="index-0" :key="index">
                     </el-option>
					 </el-select>    
				    </el-form-item>

			  </el-form>
			</div>
		</el-card>
				
		<div  style="text-align:right;margin-top:15px;">
			 <el-button @click="returnBack">返 回</el-button>
			 <el-button v-if="dialogSendData.isAdd" type="primary" @click="submitConfirmForm('dialogForm')">提 交</el-button>
		</div>
	</div>
</template>
<script>
    import agentHttp from '@/utils/agentHttp.js'
    import {require,prop} from '@/utils/rules.js'
	export default{
		created(){		
			if(this.dialogSendData.id!==""){
	      		//修改
	      		this.selectData(); 
	      	}else{
	      		this.selectBase(); 
	      	}
        },
        
        updated(){
        },
		props:['dialogSendData','tableData'],
		data(){
			return{
				
				formLabelWidth:'170px',
				type:{},
				//弹窗或者查看页面信息
		         dialogForm:{
		          maximum:1,
		          minimum:1
		        },
		        //验证规则
		        rules: {
		          'maximum':[require('最长天数',{trigger:"change",type:"number"})],
		          'minimum':[require('最短天数',{trigger:"change",type:"number"})],
		          'type':[require('借款周期类型',{trigger:"change",type:"number"})],
		        }
			}
		},methods:{
			returnBack(){
				this.dialogSendData.dialogFormVisible=false;
			},
			submitConfirmForm(formName){
                this.$refs[formName].validate((valid) => {
		          if (valid) {
		          	if(this.dialogSendData.id===""){
		          		//添加
		          	    this.addData();
		          	}else{
		          		//修改
		          		this.editData();
		          	}
		          	
		          } else {
		             this.$nextTick(()=>{
		          		this.$message.warning(document.getElementsByClassName('el-form-item__error')[0].innerText);
		                return false;
		          	})
		          }
		        });
			},
		    addData(){
		        agentHttp.postJson('adminApi/admin/car_order_duration/post_validated',this.dialogForm).then((data)=>{
		            if(data.success){  
		            	this.$emit('refreshTableData', null);
		            	this.$message.success(data.data||"添加成功");
		            	setTimeout(()=>{
		            		this.dialogSendData.dialogFormVisible=false;
		            	},500)
		            } 
		        }) 
		    },
		    editData(){
		    	
		        agentHttp.putJson('adminApi/admin/car_order_duration/put_validated',this.dialogForm).then((data)=>{
		            if(data.success){  
		            	this.$emit('refreshTableData', null);
		            	this.$message.success(data.data||"修改成功");
		            	setTimeout(()=>{
		            		this.dialogSendData.dialogFormVisible=false;
		            	},500)
		            } 
		        }) 
		    },
		    selectData(){
		        agentHttp.get('adminApi/admin/car_order_duration/select_duration/'+this.dialogSendData.id).then((data)=>{	
		            if(data.success){  
		            	this.dialogForm=data.data.carOrderDuration;
		            	this.type=data.data.type;
		            } 
		        }) 
		    },
		     selectBase(){
		        agentHttp.get('adminApi/admin/car_order_duration/base_data/').then((data)=>{	
		            if(data.success){  
		            	this.type=data.data;
		            } 
		        }) 
		    },

		}
	}
</script>
